import { TAGS_VIEW } from "@/constant"
import { getItem, setItem } from "@/utils/storage"

export default {
    namespaced: true,
    state:  () =>({
        sidebarOpened: true,
        tagsViewList: getItem(TAGS_VIEW)||[]
    }),
    mutations: {
        triggerSidebarOpened(state) {
            state.sidebarOpened = !state.sidebarOpened
        },

        // 添加tags
        addTagsViewList(state, tag){
            console.log(tag)
            const isFind = state.tagsViewList.find(item => {
                return item.path === tag.path
            })
            if(!isFind){
                state.tagsViewList.push(tag)
                setItem(TAGS_VIEW, state.tagsViewList)
            }
        },
        /** 
         * @param { type: 'other' || 'right' || 'index'} payload 
         */
        removeTagsView(state, payload) {
            if(payload.type === 'index'){
                state.tagsViewList.splice(payload.index,1)
            } else if(payload.type === 'other'){
                // 移除当前之后的所有
                state.tagsViewList.splice(payload.index + 1, state.tagsViewList.length - payload.index + 1)
                // 移除当前之前的
                state.tagsViewList.splice(0, payload.index)
            } else if(payload.type === 'right'){
                state.tagsViewList.splice(payload.index + 1, state.tagsViewList.length - payload.index + 1)
            }
            setItem(TAGS_VIEW, state.tagsViewList)
        }
    },
    actions: {}
}